﻿using System.Windows.Forms;
using Google.GData.Apps;
using Microsoft.ManagementConsole;
using Microsoft.ManagementConsole.Advanced;

namespace Google.Apps.MMC.Users
    {
    public partial class UserProperties : CustomControl
        {
        private PropertyPage parent;
        private UserNode usernode;
        private bool initdone = false;

        public UserProperties(UserNode usernode, PropertyPage page)
            {
            this.parent = page;
            this.usernode = usernode;

            InitializeComponent();
            UserEntry user = usernode.userentry;
            this.username.Text = user.Login.UserName;
            this.firstname.Text = user.Name.GivenName;
            this.lastname.Text = user.Name.FamilyName;
            this.quota.Value = user.Quota.Limit;
            if (user.Login.Admin)
                {
                this.userimage.Image = Properties.Resources.user_admin_large;
                this.type.SelectedIndex = 1;
                }
            else
                {
                this.type.SelectedIndex = 0;
                }
            initdone = true;
            }

        private void type_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                if (initdone)
                    {
                    this.parent.Dirty = true;
                    if (this.type.SelectedIndex == 1)
                        {
                        this.userimage.Image = Properties.Resources.user_admin_large;
                        }
                    else
                        {
                        this.userimage.Image = Properties.Resources.user_large;
                        }
                    }
            }


        public override bool CanApplyChanges()
            {
            MessageBoxParameters msgbox = new MessageBoxParameters();
            msgbox.Icon = MessageBoxIcon.Error;
            msgbox.Buttons = MessageBoxButtons.OK;
            msgbox.DefaultButton = MessageBoxDefaultButton.Button1;
            msgbox.Caption = "Could not apply changes";
            if (this.firstname.Text.Trim().Length == 0)
                {
                msgbox.Text = "The user cannot have a blank first name";
                this.parent.ParentSheet.ShowDialog(msgbox);
                return false;
                }
            if (this.lastname.Text.Trim().Length == 0)
                {
                msgbox.Text = "The user cannot have a blank last name";
                this.parent.ParentSheet.ShowDialog(msgbox);
                return false;
                }
            if (this.quota.Value == 0)
                {
                msgbox.Text = "The user cannot have 0MB of quota";
                this.parent.ParentSheet.ShowDialog(msgbox);
                return false;
                }
            return true;
            }

        public override bool Apply()
            {
                // Second check needed because ValueChanged Event does not fire in time to trigger apply when OK is selected with the enter key
                if (this.parent.Dirty || this.quota.Value != this.usernode.userentry.Quota.Limit)
                    {
                    usernode.userentry.Name.GivenName = this.firstname.Text;
                    usernode.userentry.Name.FamilyName = this.lastname.Text;
                    usernode.userentry.Quota.Limit = (int) this.quota.Value;
                    usernode.userentry.Login.Admin = (this.type.SelectedIndex == 1);
                    usernode.userentry.Update();
                    usernode.Update();
                    if (usernode.userentry.Quota.Limit != (int) this.quota.Value)
                        {
                        MessageBoxParameters msgbox = new MessageBoxParameters();
                        msgbox.Text = "The quota for the selected user could not be changed.\r\nThis is usually not possible except on a Partner Edition domain.";
                        msgbox.Caption = "Quota not changed";
                        msgbox.Buttons = MessageBoxButtons.OK;
                        msgbox.DefaultButton = MessageBoxDefaultButton.Button1;
                        msgbox.Icon = MessageBoxIcon.Information;
                        this.parent.ParentSheet.ShowDialog(msgbox);
                        }

                    this.username.Text = usernode.userentry.Login.UserName;
                    this.firstname.Text = usernode.userentry.Name.GivenName;
                    this.lastname.Text = usernode.userentry.Name.FamilyName;
                    this.quota.Value = usernode.userentry.Quota.Limit;
                    if (usernode.userentry.Login.Admin)
                        {
                        this.userimage.Image = Properties.Resources.user_admin_large;
                        this.type.SelectedIndex = 1;
                        }
                    else
                        {
                        this.type.SelectedIndex = 0;
                        }
                    return true;
                    }
                else
                    {
                    return true;
                    }
            }


        private void firstname_TextChanged(object sender, System.EventArgs e)
            {
                if (initdone)
                    {
                    this.parent.Dirty = true;
                    }
            }

        private void lastname_TextChanged(object sender, System.EventArgs e)
            {
                if (initdone)
                    {
                    this.parent.Dirty = true;
                    }
            }

        private void quota_ValueChanged(object sender, System.EventArgs e)
            {
                if (initdone)
                    {
                    this.parent.Dirty = true;
                    }
            }


        }
    }
